package cn.stylefeng.guns.modular.business.service.Impl;

import cn.hutool.core.util.ObjectUtil;
import cn.stylefeng.guns.modular.business.entity.CanalSecondClassify;
import cn.stylefeng.guns.modular.business.mapper.CanalSecondClassifyMapper;
import cn.stylefeng.guns.modular.business.service.CanalSecondClassifyService;
import cn.stylefeng.roses.kernel.db.api.factory.PageFactory;
import cn.stylefeng.roses.kernel.db.api.factory.PageResultFactory;
import cn.stylefeng.roses.kernel.db.api.pojo.page.PageResult;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class CanalSecondClassifyServiceImpl extends ServiceImpl<CanalSecondClassifyMapper, CanalSecondClassify> implements CanalSecondClassifyService {

    @Override
    public CanalSecondClassify detail(CanalSecondClassify canalSecondClassify) {
        return this.queryCar(canalSecondClassify);
    }

    @Override
    public PageResult<CanalSecondClassify> findPage(CanalSecondClassify canalSecondClassify) {
        LambdaQueryWrapper<CanalSecondClassify> wrapper = createWrapper(canalSecondClassify);
        Page<CanalSecondClassify> sysRolePage = this.page(PageFactory.defaultPage(), wrapper);
        return PageResultFactory.createPageResult(sysRolePage);
    }

    @Override
    public List<Map<String,Object>> findSecondList(CanalSecondClassify canalSecondClassify) {
        LambdaQueryWrapper<CanalSecondClassify> queryWrapper = new LambdaQueryWrapper<>();

        String name = canalSecondClassify.getCanalFirstClassifyCode();
        queryWrapper.eq(ObjectUtil.isNotEmpty(name), CanalSecondClassify::getCanalFirstClassifyCode, name);
        List<CanalSecondClassify> list =  this.list(queryWrapper);
        List<Map<String,Object>> mapList=new ArrayList<>();
        int i=1;
        for(CanalSecondClassify files:list) {
            Map<String, Object> map = new HashMap<String, Object>();
            map = JSONObject.parseObject(JSONObject.toJSONString(files), Map.class);
            //if(i==7){
            //    i=8;
            // }
            //map.put("icons", sysFileInfoService.preview64("eventIcon"+i+".png"));
            mapList.add(map);
            //i++;
        }
        return mapList;
    }

    /**
     * 获取信息
     */
    private CanalSecondClassify queryCar(CanalSecondClassify canalSecondClassify) {
        CanalSecondClassify secondClassfiy =this.getById(canalSecondClassify.getId());
        if (ObjectUtil.isEmpty(secondClassfiy)) {
            return null;
        }
        return secondClassfiy;
    }

    /**
     * 创建查询wrapper
     */
    private LambdaQueryWrapper<CanalSecondClassify> createWrapper(CanalSecondClassify canalSecondClassify) {
        LambdaQueryWrapper<CanalSecondClassify> queryWrapper = new LambdaQueryWrapper<>();

        String name = canalSecondClassify.getCanalFirstClassifyCode();
        queryWrapper.like(ObjectUtil.isNotEmpty(name), CanalSecondClassify::getCanalFirstClassifyCode, name);

        return queryWrapper;
    }
}
